//
//  WZAFNetTool.m
//  InsKnowTime
//
//  Created by dllo on 16/3/7.
//  Copyright © 2016年 Wayne. All rights reserved.
//

#import "WZAFNetTool.h"
#import "AFNetworking.h"
@implementation WZAFNetTool

+(void)GetNetWithURL:(NSString *)URL WithBody:(id)body headfile :(NSDictionary *)hearer response :(WZresponseStyle)responseStyle success:(void (^)(id result))success failure:(void (^)(NSError * error))failure{
    /**
     *  1.manager:设置网络管理者
     */
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 2.设置请求头
    if (hearer) {
        
        for (NSString *key in hearer.allKeys) {
            
            [manager.requestSerializer setValue:hearer[key] forHTTPHeaderField:key];
             
        }
    }
    // 3.设置数据返回的类型
    switch (responseStyle) {
        case WZJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        
        case WZXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
            
        case WZDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
            
        default:
            break;
    }
    //4.设置数据响应的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    //5.UTF-8转码
    URL = [URL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    
    path = [path stringByAppendingString:[NSString stringWithFormat:@"/%ld.plist", [URL hash]]];
  
    //6.数据请求
    [manager GET:URL parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            success(responseObject);
            if (responseObject) {
                [NSKeyedArchiver archiveRootObject:responseObject toFile:path];
                
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            
            id result =  [NSKeyedUnarchiver unarchiveObjectWithFile:path];
            if (result) {
                success(result);
            }
            
        }
    }];
}

+ (void)PostNetWithURL:(NSString *)URL WithBody:(id)body
            bodyStyle : (WZRequestStyle)bodyRequestStyle
              headfile:(NSDictionary *)hearer response:(WZresponseStyle)responseStyle success:(BlockOfSuccess)success failure:(void (^)(NSError *))failure{
    /**
     *  1.manager:设置网络管理者
     */
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //2. 设置body体数据类型
    switch (bodyRequestStyle) {
        case WZRequestJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case WZRequestString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable * _Nullable error) {
                return parameters;
            }];
            break;
        default:
            break;
    }
    // 3.设置请求头
    if (hearer) {
        for (NSString *key in hearer.allKeys) {
            [manager.requestSerializer setValue:hearer[key] forHTTPHeaderField:key];
            
        }
    }
    // 4.设置数据返回的类型
    switch (responseStyle) {
        case WZJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
            
        case WZXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
            
        case WZDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
            
        default:
            break;
    }
    
    //5.设置数据响应的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    
    //6.UTF-8转码
    URL = [URL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    //7.数据请求
    [manager POST:URL parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure(error);
        }
    }];

}



/** 自适应 */

+(CGSize)labelRectWithSize:(CGSize)size LabelText:(NSString *)labelText Font:(UIFont *)font{
    
    
    NSDictionary  *dic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
    CGSize actualsize = [labelText boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    return actualsize;
    
    
}


@end
